import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class JsoupEntrance {


    //定义路径
    static String baseUrl = "http://www.netbian.com/";
    static String getUrl = "http://www.netbian.com/desk/18321.htm";
    static String filepath = "E:\\code\\mine\\Jsoup-practice\\bizhi\\";

    public static void main(String[] args) {
        log.info("初始下载页面:{}",getUrl);
        String html = getBody(getUrl);
        List<String> srcs = getImgSrcListFromHtml(html);

        List<String> list = getNextPageUrl(html);
        log.info("下一页数量:{}",list.size());
        for(String nextPage:list){
            if(!nextPage.startsWith("http")){
                nextPage =baseUrl + nextPage;
            }
            String html1 = getBody(nextPage);
            srcs.addAll(getImgSrcListFromHtml(html1));
        }
        log.info("-------------开始下载---------------");
        for(String src:srcs){
            new JsoupEntrance().downloadImg(src,filepath);
        }
        log.info("-------------开始完成---------------");
    }

    /**
     * 获取网页的body
     * @param url
     * @return
     */
    public static String getBody(String url){
        String html = "";
        try {
            html = Jsoup.connect(url)
                    .execute()
                    .body();
        } catch (IOException e) {
            log.info("获取url出错:{}",url);
            e.printStackTrace();
        }
        return html;
    }

    /**
     * 获取页面图片的下载路径
     * @param html
     * @return
     */
    public static List<String> getImgSrcListFromHtml(String html){
        List<String> list = new ArrayList<>();
        Document document = Jsoup.parse(html);
        Elements elements = document.select("div [class=pic]").select("img");
        for(Element element:elements){
            list.add(element.attr("src"));
        }
        return list;
    }

    /**
     * 获取下一页链接
     * @param html
     * @return
     */
    public static List<String> getNextPageUrl(String html){
        List<String> list = new ArrayList<>();
        Document document = Jsoup.parse(html);
        Elements elements = document.select("div [class=list]").select("a");
        for(Element element:elements){
            list.add(element.attr("href"));
        }
        return list;
    }

    /**
     * 根据URL把图片下载到文件夹
     * @param url
     * @param filePath
     */
    public void downloadImg(String url, String filePath) {
        if(StringUtil.isBlank(url)){
            return;
        }
        log.info("图片下载地址:{}",url);
        URL newUrl = null;
        HttpURLConnection connection = null;
        byte[] bs = null;
        filePath = filePath + url.substring(url.lastIndexOf("/")+1); //获取图片名称

        try {
            newUrl = new URL(url);
            connection = (HttpURLConnection) newUrl.openConnection(); //打开连接
        }catch (Exception e){
            e.printStackTrace();
        }

        try(
                InputStream inputStream = connection.getInputStream();  //获取流
                FileOutputStream fileOutputStream = new FileOutputStream(new File(filePath));//文件输出流
        ){
            bs = getByteFromInputStream(inputStream); //流转btye[]
            log.info("生成图片路径:{}",filePath);
            fileOutputStream.write(bs); //写出

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * inputStream转换为byte
     * @param inputStream
     * @return
     */
    public byte[] getByteFromInputStream(InputStream inputStream){
        byte[] bs = null;

        byte[] buff = new byte[1024];
        int len = 0;
        try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()){
            while ((len = inputStream.read(buff)) != -1) {
                outputStream.write(buff,0,len);
            }
            bs = outputStream.toByteArray();
        }catch (IOException e){
            e.printStackTrace();
        }
        return bs;
    }
}
